<script setup lang="ts">
import APIWorks from '@/api/works'
import APIWallet from '@/api/wallet'
import type { Props, Emits } from './utils/types'
import { ElLoading, ElMessage } from 'element-plus'

const dialogVisible = shallowRef(false)
const $props = defineProps<Props>()
const $emit = defineEmits<Emits>()

watch($props, (newValue) => {
    dialogVisible.value = newValue.modelValue
}, { immediate: true })

watch(dialogVisible, (newValue) => {
    if (newValue) {
        getExposure()
        getWallet()
    }
    $emit('update:modelValue', newValue)
})

function onBeforeClose() {
    type.value = undefined
    confirmed.value = undefined
    showType.value = ''
    innerVisible.value = false
    dialogAddVisible.value = false
    dialogVisible.value = false
}

const type = shallowRef<number>()
const list = ref<any[]>([])
const loading = shallowRef<boolean>(false)

function getExposure() {
    loading.value = true
    APIWorks.getExposure().then(({ data }) => {
        list.value = data
    }).finally(() => {
        loading.value = false
    })
}

const innerVisible = shallowRef<boolean>(false)

function submitSetRewardOrder() {
    if (!type.value) return ElMessage.error('请选择打赏金额')
    innerVisible.value = true
}

const confirmed = shallowRef<any>()
const price = computed(() => {
    const result = childrenList.value.filter((item: any) => item.type == type.value)
    if (result.length != 0) {
        return result[0]
    }

    return {}
})

const walletNumber = shallowRef<number>(0)

function getWallet() {
    APIWallet.getWallet().then(({ data }) => {
        walletNumber.value = data.Wallet
    })
}

function submitPay() {
    if (!confirmed.value) return ElMessage.error('请先勾选确认订单信息，并认可《心安元素授权服务协议》及《产品退款规则》')

    const payLoading = ElLoading.service({ lock: true })

    const data: any = {
        exposures: price.value.title,
        total_amount: price.value.type,
        id: $props.goodsInfo.id,
        type: $props.goodsInfo.type,
        shop_id: $props.goodsInfo.shop_id
    }

    APIWorks.setExposure(data).then(() => {
        ElMessage.success('支付成功')
        onBeforeClose()
    }).finally(() => {
        payLoading.close()
    })
}

function onBeforeInnerClose() {
    innerVisible.value = false
}

const dialogAddVisible = shallowRef<boolean>(false)

const showType = shallowRef<string>('')

const childrenList = computed(() => {
    const result = list.value.filter(item => item.show_type == showType.value)
    if (result.length != 0) return result[0].list
    return []
})

</script>

<template>
    <el-dialog class="give clear-dialog" v-model="dialogVisible" title="广告投放端口选择" width="800" destroy-on-close
        :before-close="onBeforeClose" :show-close="false">
        <template #header>
            <DialogClose title="广告投放端口选择" @close="onBeforeClose" />
        </template>
        <div class="select-money" v-loading="loading">
            <!-- select-money-item-active -->
            <div class="select-money-item" v-for="(item, index) in list" :key="index" @click="showType = item.show_type"
                :class="{ 'select-money-item-active': showType == item.show_type }">
                {{ item.show_title }}
            </div>
        </div>
        <template #footer>
            <div>
                <el-button type="primary" @click="dialogAddVisible = true" size="large">确认</el-button>
            </div>
        </template>
        <el-dialog class="give" v-model="dialogAddVisible" width="800" destroy-on-close :show-close="false">
            <template #header>
                <DialogClose title="增加产品曝光" @close="dialogAddVisible = false" />
            </template>
            <el-dialog class="upload-blockchain" v-model="innerVisible" title="支付" width="1000" destroy-on-close
                :before-close="onBeforeInnerClose" :show-close="false">
                <template #header>
                    <DialogClose title="支付" @close="onBeforeInnerClose" />
                </template>
                <div>
                    <div class="upload-blockchain-top">
                        <div class="video">
                            <div v-if="goodsInfo?.type == 'video'">
                                <video v-if="goodsInfo?.sample" controls :src="goodsInfo.sample"></video>
                            </div>
                            <div v-if="goodsInfo?.type != 'video'">
                                <img v-if="goodsInfo?.image" :src="goodsInfo.image" alt="image">
                            </div>
                        </div>
                        <div class="video-info">
                            <div class="video-info-title">
                                产品信息：
                            </div>
                            <div class="video-info-item">
                                名 字：{{ goodsInfo?.title }}
                            </div>
                            <div class="video-info-item">
                                类 型：<span v-if="goodsInfo?.type == 'video'">视频素材</span>
                                <span v-if="goodsInfo?.type == 'picture'">图片素材</span>
                                <span v-if="goodsInfo?.type == 'music'">音乐素材</span>
                                <span v-if="goodsInfo?.type == 'font'">字体素材</span>
                            </div>
                            <div class="video-info-item">
                                编 号：{{ goodsInfo?.goods_code }}
                            </div>
                        </div>
                    </div>
                    <div class="upload-blockchain-money">
                        <div></div>
                        <div>本订单才有余额抵扣（当前账户余额 <span>￥{{ walletNumber }}</span> 元）
                            请保证有足够的余额，余额不够时，请点击“<span>充值</span>”来增加余额）</div>
                    </div>
                    <div class="upload-blockchain-sum">
                        <div>应付金额：</div>
                        <div>￥{{ price.type }}</div>
                    </div>
                    <div class="upload-blockchain-affirm">
                        <el-radio :value="true" size="large" v-model="confirmed" text-color="#606266">
                            <div style="color: #606266;">确认订单信息，并认可<span class="active">《心安元素授权服务协议》</span>及<span
                                    class="active">《产品退款规则》</span></div>
                        </el-radio>
                    </div>
                    <div class="upload-blockchain-btn">
                        <button @click="submitPay">支付</button>
                    </div>
                </div>

            </el-dialog>
            <div class="select-money">
                <div class="select-money-item " @click="type = item.type" v-for="item in childrenList" :key="item.id"
                    :class="{ 'select-money-item-active': type == item.type }">
                    {{ item.type }}/{{ item.title }}次
                </div>
            </div>
            <template #footer>
                <div>
                    <el-button type="primary" @click="submitSetRewardOrder" size="large">确认</el-button>
                </div>
            </template>
        </el-dialog>
    </el-dialog>
</template>

<style lang="scss" scoped>
.upload-blockchain {

    .upload-blockchain-btn {
        display: flex;

        button {
            width: 176px;
            height: 50px;
            border-radius: 5px;
            background-color: rgba(249, 20, 20, 1);
            color: rgba(255, 255, 255, 1);
            font-size: 14px;
            text-align: center;
            border: none;
            cursor: pointer;
            margin-left: auto;
            margin-right: 42px;
        }
    }

    .upload-blockchain-affirm {
        display: flex;
        justify-content: flex-end;
        margin-right: 42px;

        .active {
            color: #3894FF;
        }
    }

    .upload-blockchain-sum {
        display: flex;
        justify-content: flex-end;
        align-items: flex-end;
        margin-right: 42px;
        margin-top: 10px;

        div {
            &:nth-child(1) {
                color: rgba(16, 16, 16, 1);
                font-size: 14px;
            }

            &:nth-child(2) {
                color: rgba(249, 20, 20, 1);
                font-size: 36px;
                line-height: 1em;
                font-weight: 700;
            }
        }
    }

    .upload-blockchain-money {
        display: flex;
        justify-content: center;
        gap: 0 16px;
        margin-top: 56px;

        div {
            &:nth-child(1) {
                width: 4px;
                height: 50px;
                background-color: #FF0000;
            }

            &:nth-child(2) {
                width: 860px;
                height: 50px;
                line-height: 50px;
                border-radius: 5px;
                background-color: rgba(238, 238, 239, 1);
                border: 1px dashed rgba(187, 187, 187, 1);
                font-size: 14px;
                color: #3894FF;
                text-align: center;

                span {
                    color: #F91414;
                    cursor: pointer;
                }
            }
        }
    }

    .upload-blockchain-top {
        display: flex;
        gap: 0 44px;

        .video-info {
            color: rgba(16, 16, 16, 1);

            .video-info-title {
                color: rgba(16, 16, 16, 1);
                font-size: 14px;
                display: flex;
                align-items: center;
                line-height: 20px;
                margin-bottom: 10px;

                &::before {
                    content: '';
                    display: block;
                    width: 3px;
                    height: 14px;
                    background-color: #FF0000;
                    margin-right: 8px;
                }
            }

            .video-info-item {
                color: rgba(16, 16, 16, 1);
                line-height: 35px;
                font-size: 14px;
            }
        }

        .video {
            width: 256px;
            height: 256px;
            background-color: #eeeeee;
            display: flex;
            align-items: center;

            div {
                width: 100%;
                height: 100%;

                video {
                    width: 100%;
                    height: 100%;
                    object-fit: contain;
                }

                img {
                    width: 100%;
                    height: 100%;
                    object-fit: cover;
                }
            }


        }
    }

}

.give {
    .select-money {
        display: grid;
        grid-template-columns: repeat(6, 1fr);
        margin-top: 30px;
        gap: 30px 0;

        div.select-money-item-active {
            background-color: rgba(56, 148, 255, 1);
            color: #ffffff;
        }

        .select-money-item {
            width: 120px;
            height: 50px;
            line-height: 50px;
            border-radius: 5px;
            font-size: 14px;
            text-align: center;
            color: #888888;
            background-color: rgba(238, 238, 239, 1);
            cursor: pointer;
        }
    }
}
</style>
